package dk.rhok.findstufffast;



import com.google.android.maps.*;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.*;
import android.location.*;
import android.content.Context;

import java.util.*;



public class FindStuffFast extends MapActivity implements LocationListener
{
    private LocationManager locationManager;
    private LocationDatabase locationDatabase;

    private Compass compass;

    private int prevViewManagerIdx;
    private ViewManager[] viewManagers;

    PowerManager pm;
    PowerManager.WakeLock lock;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        lock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Tag");

        viewManagers = new ViewManager[]
        {
            new PicturePictureManager(),
            new MapPictureManager(this),
            new ArrowManager(this)
            //new MapManager(this)
        };
        prevViewManagerIdx = viewManagers.length - 1;
        setContentView(viewManagers[prevViewManagerIdx].getLayoutId());
        viewManagers[prevViewManagerIdx].setEnabled(true);



        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);

        //FIXME: Set polling interval
        //final int MIN_TIME = 60 * 1000; //One minute.
        final int MIN_TIME = 0;
                
        locationManager.requestLocationUpdates(provider, MIN_TIME, 0, this);

        

        locationDatabase = new LocationDatabase(this);

        compass = new Compass(this);
        compass.setListener(viewManagers[prevViewManagerIdx]);
    }



    public void onResume() {
    	super.onResume();
    	lock.acquire();
    }



    public void onLocationChanged(Location phoneLocation)
    {
        Location targetLocation = locationDatabase.getBestLocation(phoneLocation);
        double dist = targetLocation.distanceTo(phoneLocation);
        System.out.println("          Dist: " + dist);
        int viewManagerIdx = prevViewManagerIdx;
        while(dist < viewManagers[viewManagerIdx].getMinDistance())
        {
            viewManagerIdx--;
            System.out.println("          Prev min dist: " + viewManagers[viewManagerIdx].getMinDistance());
            System.out.println("          Decreased to: " + viewManagerIdx);
        }
        while(dist > viewManagers[viewManagerIdx].getMaxDistance())
        {
            viewManagerIdx++;
            System.out.println("          Increased to: " + viewManagerIdx);
        }

        ViewManager viewManager = viewManagers[viewManagerIdx];

        if(viewManagerIdx != prevViewManagerIdx)
        {
            viewManagers[prevViewManagerIdx].setEnabled(false);
            setContentView(viewManager.getLayoutId());
            viewManager.setEnabled(true);
            compass.setListener(viewManager);
        }

        viewManager.update(phoneLocation, targetLocation);            

        prevViewManagerIdx = viewManagerIdx;
    }



    public void onProviderDisabled(String provider)
    {
    }



    public void onProviderEnabled(String provider)
    {
    }



    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }


    
    public void onPause() {
    	if (lock.isHeld()){
    		lock.release();
    	}
    	super.onPause();
    }


    
    public void onStop() {	
    	if (lock.isHeld()){
    		lock.release();
    	}
    	super.onStop();
    }



    public Compass getCompass()
    {
        return compass;
    }



    @Override
    public boolean isRouteDisplayed()
    {
        return false;
    }
}
